package best.driver.android.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import best.driver.android.AppConfig;
import best.driver.android.Beans.DriverDetail;
import best.driver.android.Beans.DriverInfo;
import best.driver.android.BestApp;


public class UserInfoManager {

    private Context mContext;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private static class SettingManagerHolder {
        private static final UserInfoManager instance = new UserInfoManager();
    }

    public static UserInfoManager getInstance() {
        return SettingManagerHolder.instance;
    }

    private UserInfoManager() {
        init(BestApp.getInstance().getApplicationContext());
    }

    public void init(Context context) {
        if (context != null) {
            mContext = context.getApplicationContext();
        }
        int mode = (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) ? Context.MODE_MULTI_PROCESS : Context.MODE_PRIVATE;
        mSharedPreferences = mContext.getSharedPreferences("user_info", mode);
        mEditor = mSharedPreferences.edit();
    }

    public void setToken(String token) {
        mEditor.putString("token", "AT_D_"+token).commit();
    }

    public void setDriverId(String driverId) {
        mEditor.putString("driverId", driverId).commit();
        Log.d("DriverID", driverId);
    }

    public void setAcceptOrderStatus(boolean status) {
        mEditor.putBoolean("accept_order_status", status).commit();
    }

    public boolean getAcceptOrderStatus() {
        return mSharedPreferences.getBoolean("accept_order_status", false);
    }

    public String getDriverId() {
        return mSharedPreferences.getString("driverId", "");
    }

    public String getToken() {
        return mSharedPreferences.getString("token", "");
    }

    public void setCarPicId(String id) {
        mEditor.putString("truckPhoto", id).commit();
    }

    public String getCarPicId() {
        return mSharedPreferences.getString("truckPhoto", "");
    }

    public void setIdentifyFrontId(String id) {
        mEditor.putString("identityFrontPhoto", id).commit();
    }

    public String getIdentifyFrontId() {
        return mSharedPreferences.getString("identityFrontPhoto", "");
    }

    public void setIdentifyBackId(String id) {
        mEditor.putString("identityBackPhoto", id).commit();
    }

    public String getIdentifyBackId() {
        return mSharedPreferences.getString("identityBackPhoto", "");
    }

    public void setDrivingCarPicId(String id) {
        mEditor.putString("driverLicensePhoto", id).commit();
    }

    public String getDrivingCarPicId() {
        return mSharedPreferences.getString("driverLicensePhoto", "");
    }

    public void setOnRoadPicId(String id) {
        mEditor.putString("truckLicensePhoto", id).commit();
    }

    public String getOnRoadPicId() {
        return mSharedPreferences.getString("truckLicensePhoto", "");
    }

    public void setOperationPicId(String id) {
        mEditor.putString("operationLicensePhoto", id).commit();
    }

    public String getOperationPicId() {
        return mSharedPreferences.getString("operationLicensePhoto", "");
    }



    public void clearUserInfo(){
        setToken(null);
        setCheckStatus(false);
        setLogin(false);
        setAcceptOrderStatus(false);
        mEditor.clear();
        mEditor.commit();
    }

    public void setIsFirstSetUp(Boolean status) {
        mEditor.putBoolean("isFirstSetUp", status);
        mEditor.commit();
    }


    public boolean getIsFirstSetUp() {
        return mSharedPreferences.getBoolean("isFirstSetUp", true);
    }

    public void setLogin(boolean status) {
        mEditor.putBoolean("isLogin", status);
        mEditor.commit();
    }

    public boolean isLogin() {
        return mSharedPreferences.getBoolean("isLogin", false);
    }

    public void setCheckStatus(boolean status) {
        mEditor.putBoolean("check_status", status);
        mEditor.commit();
    }

    public void setDriverInfo(DriverDetail driverInfo) {
        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.driverLicensePhoto)) {
            mEditor.putString("driverLicensePhoto", driverInfo.driverAuditInfo.driverLicensePhoto);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.identityBackPhoto)) {
            mEditor.putString("identityBackPhoto", driverInfo.driverAuditInfo.identityBackPhoto);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.identityCard)) {
            mEditor.putString("identityCard", driverInfo.driverAuditInfo.identityCard);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.identityFrontPhoto)) {
            mEditor.putString("identityFrontPhoto", driverInfo.driverAuditInfo.identityFrontPhoto);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.operationLicensePhoto)) {
            mEditor.putString("operationLicensePhoto", driverInfo.driverAuditInfo.operationLicensePhoto);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.truckLicensePhoto)) {
            mEditor.putString("truckLicensePhoto", driverInfo.driverAuditInfo.truckLicensePhoto);
        }

        if(!TextUtils.isEmpty(driverInfo.driverAuditInfo.truckPhoto)) {
            mEditor.putString("truckPhoto", driverInfo.driverAuditInfo.truckPhoto);
        }
        mEditor.putInt("orderCount",driverInfo.orderCount);
        mEditor.putString("truckName", driverInfo.truckName);
        mEditor.putString("vehicleId",driverInfo.vehicleId);
        mEditor.putString("phone",driverInfo.phone);
        mEditor.putString("truckNum",driverInfo.truckNum);
        mEditor.putString("name",driverInfo.name);
        mEditor.putFloat("credit", (float) driverInfo.credit);
        mEditor.putString("driverType", driverInfo.driverType);
        mEditor.putString("driverPhoto", driverInfo.driverPhoto);
        mEditor.putString("cityId", driverInfo.cityId);
        mEditor.putString("auditStatus", driverInfo.auditStatus);
        mEditor.commit();
    }

    public DriverInfo getDriverInfo() {
        DriverInfo info = new DriverInfo();
        info.driverLicensePhoto = mSharedPreferences.getString("driverLicensePhoto","");
        info.identityBackPhoto = mSharedPreferences.getString("identityBackPhoto","");
        info.identityCardId = mSharedPreferences.getString("identityCard","");
        info.identityFrontPhoto = mSharedPreferences.getString("identityFrontPhoto","");
        info.operationLicensePhoto = mSharedPreferences.getString("operationLicensePhoto","");
        info.truckLicensePhoto = mSharedPreferences.getString("truckLicensePhoto","");
        info.truckPhoto = mSharedPreferences.getString("truckPhoto", "");
        info.truckPhoto = mSharedPreferences.getString("truckPhoto","");
        info.orderCount = mSharedPreferences.getInt("orderCount", 0);
        info.truckName = mSharedPreferences.getString("truckName", "");
        info.vehicleId = mSharedPreferences.getString("vehicleId", "");
        info.phone = mSharedPreferences.getString("phone","");
        info.truckNum = mSharedPreferences.getString("truckNum","");
        info.name = mSharedPreferences.getString("name","");
        info.credit = mSharedPreferences.getFloat("credit", 0);
        info.driverType = mSharedPreferences.getString("driverType", "");
        info.driverPhoto = mSharedPreferences.getString("driverPhoto","");
        info.cityId = mSharedPreferences.getString("cityId", "");
        info.auditStatus = mSharedPreferences.getString("auditStatus", "");
        return info;

    }

    public boolean isCheckSucceed() {
        return mSharedPreferences.getBoolean("check_status", false);
    }

    public void setEnvironmentCode(int code) {
        mEditor.putInt("develop_environment", code).commit();
    }

    public int getEnvironmentCode() {
        return mSharedPreferences.getInt("develop_environment", AppConfig.ContextMode.ONLINE.ordinal());
    }



}
